require('babel-register');

const webpack = require('webpack');
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const config = require('./webpack.config');
const app = express();
var port = 80;
const compiler = webpack(config);
if (process.env.NODE_ENV == 'development') {
// Webpack developer
    port = 3000;
    app.use(require('webpack-dev-middleware')(compiler, {
        publicPath: config.output.publicPath,
        noInfo: true
    }));
    app.use(require('webpack-hot-middleware')(compiler));
}

//  RESTful API
const publicPath = path.resolve(__dirname);
app.use(bodyParser.json({type: 'application/json'}));
app.use(express.static(publicPath));

// this is necessary to handle URL correctly since client uses Browser History
app.get('*', function (req, res) {
    res.sendFile(path.resolve(__dirname, '', 'index.html'))
});


app.listen(port, function (err, result) {
    if (err) {
        console.log(err);
    }
    console.log('Server running on port ' + port);
});
